package cn.tqfeiyang.springplus.warehouse.service.impl;

import cn.tqfeiyang.springplus.warehouse.entity.CustomerEntity;
import cn.tqfeiyang.springplus.warehouse.mapper.CustomerMapper;
import cn.tqfeiyang.springplus.warehouse.service.CustomerService;
import cn.tqfeiyang.springplus.warehouse.request.CustomerDTO;
import cn.tqfeiyang.springplus.warehouse.response.CustomerVO;
import cn.tqfeiyang.springplus.warehouse.request.CustomerSTO;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.tqfeiyang.springplus.framework.conversion.ConversionService;
import cn.tqfeiyang.springplus.framework.exception.BusinessException;
import cn.tqfeiyang.springplus.framework.response.ResultCode;
import cn.tqfeiyang.springplus.framework.pagination.PageHelper;
import cn.tqfeiyang.springplus.framework.pagination.PageData;
import jakarta.annotation.Resource;
import org.springframework.util.Assert;

/**
 * 客户服务实现类
 *
 * @author tqfeiyang
 * @since 2024-08-01
 */
@Service
public class CustomerServiceImpl extends ServiceImpl<CustomerMapper, CustomerEntity> implements CustomerService {

    /**
     * 对象转换服务
     */
    @Resource
    private ConversionService conversionService;

    /**
     * 客户映射接口
     */
    @Resource
    private CustomerMapper customerMapper;

    /**
     * 分页查询客户
     *
     * @param customerSTO 查询数据
     * @return 客户列表
     */
    @Override
    public PageData<CustomerVO> selectCustomerPage(CustomerSTO customerSTO) {
        Assert.notNull(customerSTO, "customerSTO不能为空");
        PageHelper<CustomerEntity> pageHelper = new PageHelper<>(customerSTO);
        return pageHelper.selectPage(customerMapper, CustomerVO.class);
    }

    /**
     * 查询客户
     *
     * @param companyId 客户id
     * @return 客户信息
     */
    @Override
    public CustomerVO selectCustomerInfo(Long companyId) {
        Assert.notNull(companyId, "companyId不能为空");
        CustomerEntity customerEntity = checkCustomerExists(companyId);
        return conversionService.map(customerEntity, CustomerVO.class);
    }

    /**
     * 新建客户
     *
     * @param customerDTO 请求数据
     * @return 客户id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long createCustomer(CustomerDTO customerDTO) {
        Assert.notNull(customerDTO, "customerDTO不能为空");
        CustomerEntity customerEntity = conversionService.map(customerDTO, CustomerEntity.class);
        if (customerMapper.insert(customerEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_INSERT_ERROR);
        }
        return customerEntity.getCompanyId();
    }

    /**
     * 修改客户
     *
     * @param companyId 客户id
     * @param customerDTO 请求数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCustomer(Long companyId, CustomerDTO customerDTO) {
        Assert.notNull(companyId, "companyId不能为空");
        Assert.notNull(customerDTO, "customerDTO不能为空");
        CustomerEntity originalEntity = checkCustomerExists(companyId);
        CustomerEntity customerEntity = conversionService.map(customerDTO, CustomerEntity.class);
        customerEntity.setCompanyId(companyId);
        customerEntity.setVersion(originalEntity.getVersion());
        if (customerMapper.updateById(customerEntity) == 0) {
            throw new BusinessException(ResultCode.ENTITY_UPDATE_ERROR);
        }
    }

    /**
     * 删除客户
     *
     * @param companyId 客户id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCustomer(Long companyId) {
        Assert.notNull(companyId, "companyId不能为空");
        checkCustomerExists(companyId);
        if (customerMapper.deleteById(companyId) == 0) {
            throw new BusinessException(ResultCode.ENTITY_DELETE_ERROR);
        }
    }

    /**
     * 检查客户实体是否存在
     *
     * @param companyId 客户id
     * @return 客户实体
     */
    protected CustomerEntity checkCustomerExists(Long companyId) {
        CustomerEntity customerEntity = customerMapper.selectById(companyId);
        if (customerEntity == null) {
            throw new BusinessException(ResultCode.ENTITY_NOT_EXISTS);
        }
        return customerEntity;
    }

}
